package com.example.mvpdemo.net;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.mvpdemo.R;

/***
 * 网络加载提示框
 */
public class NetLoadingDialog extends Dialog {

    private Context mContext;
    private ImageView ivProgress;
    private TextView tvInfo;

    public NetLoadingDialog(Context mContext) {
        super(mContext, R.style.LoadingDialog);
        this.mContext = mContext;
        initDialog();
    }

    private void initDialog() {
        setContentView(R.layout.view_loading_dialog);
        ivProgress = findViewById(R.id.img);
        tvInfo = findViewById(R.id.tv_tip);
    }

    /***
     * 设置文字
     * @param tip 文本内容
     */
    public void setTextTip(String tip) {
        if (tip == null || tip.isEmpty()) {
            tvInfo.setVisibility(View.GONE);
        } else {
            tvInfo.setVisibility(View.VISIBLE);
            tvInfo.setText(tip);
        }
    }

    /***
     * 显示加载框
     * @param text 显示文本
     * @param isClickCancle 点击是否取消dialog
     */
    public void showDialog(String text, boolean isClickCancle) {
        setCanceledOnTouchOutside(isClickCancle);
        setTextTip(text);
        Animation animation = AnimationUtils.loadAnimation(mContext,
                R.anim.anim_dialog_load);
        // 显示动画
        ivProgress.startAnimation(animation);
        show();

    }

    /**
     * 取消对话框
     */
    public void closeDialog() {
        ivProgress.clearAnimation();
        dismiss();
    }
}
